package com.wecon.dianping.service.impl;

import com.wecon.dianping.common.BusinessException;
import com.wecon.dianping.common.EmBusinessError;
import com.wecon.dianping.dal.SellerMapper;
import com.wecon.dianping.model.Seller;
import com.wecon.dianping.model.SellerExample;
import com.wecon.dianping.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Weikang  Wang
 * @program: com.wecon.dianping.service.impl
 * @description: 商家服务层
 * @date 2020/3/17 14:43
 */
@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerMapper sellerMapper;

    /**
     * 创建商家
     *
     * @param seller 商家
     * @return /
     */
    @Override
    @Transactional
    public Seller create(Seller seller) {
        seller.setRemarkScore(new BigDecimal(0));
        seller.setDisabledFlag(0);
        sellerMapper.insertSelective(seller);

        return get(seller.getId());
    }

    /**
     * 根据id获取商家
     *
     * @param id id
     * @return /
     */
    @Override
    public Seller get(Integer id) {
        return sellerMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取商户列表
     *
     * @return /
     */
    @Override
    public List<Seller> selectAll() {
        SellerExample example = new SellerExample();
        example.or().andIdGreaterThan(0);
        return sellerMapper.selectByExample(example);
    }

    /**
     * 改变商户状态
     *
     * @param id           id
     * @param disabledFlag 状态
     * @return /
     * @throws BusinessException /
     */
    @Override
    public Seller changeStatus(Integer id, Integer disabledFlag) throws BusinessException {
        Seller seller = get(id);
        if (seller == null) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR);
        }
        seller.setDisabledFlag(disabledFlag);
        sellerMapper.updateByPrimaryKeySelective(seller);
        return seller;
    }

    /**
     * 获取商户数量
     * @return /
     */
    @Override
    public Integer countAllSeller() {
        SellerExample example = new SellerExample();
        example.or().andIdGreaterThan(0);
        return sellerMapper.countByExample(example);
    }
}
